//
//  XTTZController.m
//  FruitsWholesaler
//
//  Created by sgone-2 on 15/11/29.
//  Copyright © 2015年 sgone. All rights reserved.
//

#import "XTTZController.h"
#import "AFNetworking.h"
#import "MBProgressHUD.h"
#import "MJRefresh.h"
#import "UIView+Helper.h"
#import "OMOrderEmptyTableViewCell.h"
#import "XXTZCell.h"

#import "PCMyMessageDetailsViewController.h"


@interface XTTZController () <UITableViewDelegate, UITableViewDataSource>


{
    
    CGFloat cellHeight;
    int currentPage;
    int sizeCount;
    CGFloat emptyCellHeight;
}



@property(nonatomic,strong) AFHTTPSessionManager *manager;


@property (strong, nonatomic) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong)  XXTZCell*cell;
@property (nonatomic, strong) OMOrderEmptyTableViewCell *emptyCell;


@end

@implementation XTTZController



//afnetworking网络请求数据
- (AFHTTPSessionManager *)manager
{
    if (_manager == nil) {
        _manager  = [AFHTTPSessionManager manager];
    }
    return _manager;
    
    
}


- (NSMutableArray *)dataArray {
    
    if (_dataArray == nil) {
        
        _dataArray = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _dataArray;
}



- (void)viewDidLoad {
    [super viewDidLoad];


    self.titleLabel.text = @"我的消息";
    self.backBtn.hidden = NO;
    
    [self creatUI];
    [self creatData];
    
}


-(void)creatUI
{
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, ViewWidth, ViewHeight - 64) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = FDBack_COLOR;
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];




}



-(void)creatData
{

    // 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)
    [self.tableView addHeaderWithTarget:self action:@selector(headerRereshing)];
    
    //#warning 自动刷新(一进入程序就下拉刷新)
    [self.tableView headerBeginRefreshing];
    
    
    // 2.上拉加载更多(进入刷新状态就会调用self的footerRereshing)
    [self.tableView addFooterWithTarget:self action:@selector(footerRereshing13)];
    
    // 设置文字(也可以不设置,默认的文字在MJRefreshConst中修改)
    self.tableView.headerPullToRefreshText = @"下拉可以刷新了";
    self.tableView.headerReleaseToRefreshText = @"松开马上刷新了";
    self.tableView.headerRefreshingText = @"正在帮你刷新中";


}



- (void)headerRereshing {
    
    sizeCount = 20;
    currentPage = 1;
    [self getInquiryMessageListByService:currentPage size:sizeCount];




}



- (void)getInquiryMessageListByService:(int)page size:(int)size {
    
 
//    [MBProgressHUD showHUDAddedTo:self.tableView animated:YES];
    NSString *urlStr = [NSString stringWithFormat:@"%@chart_log_list",SERVER_ADDRESS3];
    NSDictionary *pramas = @{@"cur_page" : [NSString stringWithFormat:@"%d",page] , @"cur_size" : [NSString stringWithFormat:@"%d",size] , @"uid" : [self.dataHandler.userDic objectForKey:@"wId"] , @"user_type" : @"3", @"chart_type" : @"2" };
    [self.manager POST:urlStr parameters:pramas success:^(NSURLSessionDataTask *task, id responseObject) {
       
        
        
        
        if ([responseObject[@"error_flag"] intValue] == 0) {
            
            
            [MBProgressHUD hideHUDForView:self.tableView animated:YES];
            [self.tableView headerEndRefreshing];
            NSArray *array = responseObject[@"data"][@"list"];
            [self.dataArray removeAllObjects];
            [self.dataArray addObjectsFromArray:array];
            [self.tableView reloadData];

        }
        else
        {
            [MBProgressHUD hideHUDForView:self.tableView animated:YES];
            [self.tableView headerEndRefreshing];

            NSString *str = responseObject[@"result_msg"];
            [self.view makeToast:str];
        
        
        }
        
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        [MBProgressHUD hideHUDForView:self.tableView animated:YES];
        [self.tableView headerEndRefreshing];

        [self.view makeToast:@"请检查网络"];

        
        
    }];
    
}


-(void)footerRereshing13
{

    currentPage++;
    sizeCount = 20;

    
    
//    [MBProgressHUD showHUDAddedTo:self.tableView animated:YES];
    NSString *urlStr = [NSString stringWithFormat:@"%@chart_log_list",SERVER_ADDRESS3];
    NSDictionary *pramas = @{@"cur_page" : [NSString stringWithFormat:@"%d",currentPage] , @"cur_size" : [NSString stringWithFormat:@"%d",sizeCount] , @"uid" : [self.dataHandler.userDic objectForKey:@"wId"] , @"user_type" : @"3", @"chart_type" : @"2" };
    [self.manager POST:urlStr parameters:pramas success:^(NSURLSessionDataTask *task, id responseObject) {
        
        
        
        if ([responseObject[@"error_flag"] intValue] == 0) {
            
            
            [MBProgressHUD hideHUDForView:self.tableView animated:YES];
            NSArray *array = responseObject[@"data"][@"list"];
            
            
            
            if (array.count == 0) {
                
                self.tableView.footerReleaseToRefreshText = @"暂无更多数据";
                self.tableView.footerRefreshingText = @"暂无更多数据";
            }
            else
            {
                
                [_dataArray addObjectsFromArray:array];
                
            }
            
            [self.tableView reloadData];
            [self.tableView footerEndRefreshing];
            
            
        }
        else
        {
            [MBProgressHUD hideHUDForView:self.tableView animated:YES];
            [self.tableView footerEndRefreshing];

            NSString *str = responseObject[@"result_msg"];
            [self.view makeToast:str];
            
            
        }
        
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        [MBProgressHUD hideHUDForView:self.tableView animated:YES];
        [self.tableView footerEndRefreshing];

        [self.view makeToast:@"请检查网络"];
        
        
        
    }];

}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    if (self.dataArray.count == 0) {
        
        return 1;
    }
    return self.dataArray.count;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 5;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 0.01;
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.dataArray.count == 0) {
        
        return emptyCellHeight;
        
    }
    return cellHeight;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.dataArray.count == 0) {
        
        self.emptyCell = [OMOrderEmptyTableViewCell cellWithTableView:self.tableView];
        self.emptyCell.selectionStyle = UITableViewCellSelectionStyleNone;
        emptyCellHeight = [self.emptyCell setEmptyOrderCell];
        return self.emptyCell;
    }
    self.cell = [XXTZCell cellWithTableView:tableView];
    self.cell.selectionStyle = UITableViewCellSelectionStyleNone;
    self.cell.messageDic = self.dataArray[indexPath.section];
    self.cell.row = indexPath.row;
    cellHeight = [self.cell getCellHeight];
    
    return self.cell;
}




- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    PCMyMessageDetailsViewController *detailsVC = [[PCMyMessageDetailsViewController alloc] initWithNibName:nil bundle:nil];
    detailsVC.chart_id = self.dataArray[indexPath.section][@"id"];
    
    detailsVC.p_uid = self.dataArray[indexPath.section][@"p_uid"];
    detailsVC.p_user_type = self.dataArray[indexPath.section][@"p_user_type"];
    
    
    detailsVC.dict = self.dataArray[indexPath.section];
    [self.navigationController pushViewController:detailsVC animated:YES];
}

@end
